package services;

import controllers.ViewManager;
import entity.Business;
import entity.Customer;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import utils.MD5Util;

import java.io.IOException;

/**
 * @ClassName LoginService
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/17 14:34
 */
public class LoginService {


    public Boolean checkPassword(Boolean isCustomer,Object user,String pwd){
        SystemContext systemContext=SystemContext.getInstance();
        if(isCustomer){
            Customer customer=(Customer)user;
            if(!customer.getPassword().equals(MD5Util.MD5(pwd)))
                return false;
            systemContext.setId(customer.getCustomer_id());
        }else {
            Business business=(Business)user;
            if(!business.getPassword().equals(MD5Util.MD5(pwd)))
                return false;
            systemContext.setId(business.getBusiness_id());
        }
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                JumpToSystemView(isCustomer);
            }
        });
        return true;
    }

    public void JumpToSystemView(Boolean isCustomerView){
        Stage SystemStage=ViewManager.STAGE.get("primaryStage");
        if(isCustomerView){
            SystemStage.setTitle("CustomerView");
            SystemContext.getInstance().setIsCustomer(true);
        }else{
            SystemStage.setTitle("BusinessView");
            SystemContext.getInstance().setIsCustomer(false);
        }
        Parent root;
        try {
            root= FXMLLoader.load(getClass().getResource("/views/SystemView.fxml"));
            SystemStage.setScene(new Scene(root));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(SystemContext.getInstance().getId());
    }


}
